package com.flower.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.flower.subject.domain.entity.SubjectLabelBO;
import com.flower.subject.domain.entity.SubjectLikedBO;
import com.flower.subject.domain.redis.RedisUtil;
import com.flower.subject.domain.service.SubjectLikedDomainService;
import com.flower.subject.enums.SubjectLikeStatusEnum;
import com.flower.subject.infra.basic.dao.SubjectLikedDao;
import com.flower.subject.infra.basic.entity.SubjectLiked;
import com.flower.subject.infra.basic.service.SubjectLikedService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (SubjectLiked)表服务实现类
 *
 * @author makejava
 * @since 2024-07-28 22:04:12
 */
@Service
@Slf4j
public class SubjectLikedDomainServiceImpl implements SubjectLikedDomainService {
    @Resource
    private SubjectLikedDao subjectLikedDao;


    @Resource
    private SubjectLikedService subjectLikedService;

    @Resource
    private RedisUtil redisUtil;

    private static final String SUBJECT_LIKED_KEY = "subject.liked";
    private static final String SUBJECT_LIKED_COUNT_KEY = "subject.liked.count";

    private static final String SUBJECT_LIKED_DETAIL_KEY = "subject.liked.detail";



    @Override
    public Boolean add(SubjectLikedBO subjectLikedBO) {
        Long subjectId = subjectLikedBO.getSubjectId();
        Long likeUserId = subjectLikedBO.getLikeUserId();
        Integer status = subjectLikedBO.getStatus();
        // 存放点赞状态
        String hashKey = buildSubjectLikedKey(subjectId.toString(), likeUserId.toString());
        redisUtil.putHash(SUBJECT_LIKED_KEY, hashKey, status);
        // 存放总赞数
        String countKey = SUBJECT_LIKED_COUNT_KEY + "." + subjectId;

        // 记录点赞
        String detailKey = SUBJECT_LIKED_DETAIL_KEY + "." + subjectId + "." + likeUserId;

        if(SubjectLikeStatusEnum.LIKED.getCode() == status){
            redisUtil.increment(countKey,1);
            redisUtil.set(detailKey,"1");
        }else {
            Integer count = redisUtil.getInt(countKey);
            if(Objects.isNull(count) || count <= 0){

            }else {
                redisUtil.increment(countKey,-1);
                redisUtil.del(detailKey);
            }

        }
        return null;
    }

    @Override
    public Boolean isLiked(String subjectId, String userId) {
        String detailKey = SUBJECT_LIKED_DETAIL_KEY + "." + subjectId + "." + userId;

        return redisUtil.exist(detailKey);
    }

    @Override
    public Integer getCount(String subjectId) {
        String countKey = SUBJECT_LIKED_COUNT_KEY + "." + subjectId;
        Integer count = redisUtil.getInt(countKey);
        if(Objects.isNull(count) || count <= 0){
            return 0;
        }
        return count;
    }

    @Override
    public void syncLiked() {

        Map<Object,Object> subjectLikedMap =  redisUtil.getHashAndDelete(SUBJECT_LIKED_KEY);
        if (log.isInfoEnabled()){
            log.info("syncLiked.subjectLikedMap:{}", JSON.toJSONString(subjectLikedMap));
        }

        if(MapUtils.isEmpty(subjectLikedMap)){
            return;
        }
        // 批量同步到数据库
        List<SubjectLiked> subjectLikedList = new LinkedList<>();
        subjectLikedMap.forEach((k,v)->{
            SubjectLiked subjectLiked = new SubjectLiked();
            String[] keyArr = k.toString().split(":");
            String subjectId = keyArr[0];
            String likeUserId = keyArr[1];
            subjectLiked.setSubjectId(Long.valueOf(subjectId));
            subjectLiked.setLikeUserId(Long.valueOf(likeUserId));
            subjectLiked.setStatus(Integer.valueOf(v.toString()));
            subjectLikedList.add(subjectLiked);
        });

        subjectLikedService.batchInsert(subjectLikedList);
    }

    private String buildSubjectLikedKey(String subjectId, String userId) {
        return subjectId + ":" + userId;
    }
}
